package timbap.visualizer.coreplugins.lib.artworkhue;

import java.awt.*;

/**
 * The <code>HueComparableColor</code> class represents
 * a color that can be compared and thus sorted by hue.
 *
 * @author  Andreas Pabst
 * @see     java.awt.Color
 */
public class HueComparableColor extends Color implements Comparable<Color> {

    public static final float SATURATION_LOWER_THRESHOLD = 0.25f;
    public static final float BRIGHTNESS_LOWER_THRESHOLD = 0.15f;
    public static final float BRIGHTNESS_UPPER_THRESHOLD = 0.85f;

    private static final long serialVersionUID = 1L;

    private Long artworkDataChecksum;

    public HueComparableColor(Color pColor, Long pArtworkDataChecksum) {
        super(pColor.getRGB());
        artworkDataChecksum = pArtworkDataChecksum;
    }
    
    public Long getArtworkDataChecksum() {
        return artworkDataChecksum;
    }

    public int compareTo(Color o) {
        if (o != null) {
        	if (hasEnoughColor(this)) {
        		if (hasEnoughColor(o)) {
        			Float hueOfThis = RGBtoHSB(this.getRed(), this.getGreen(), this.getBlue(), null)[0];
        			Float hueOfO = RGBtoHSB(o.getRed(), o.getGreen(), o.getBlue(), null)[0];
        			return (hueOfThis.compareTo(hueOfO));
        		} else {
        			return 0;
        		}
        	} else {
        		return 1;
        	}
        } else {
            return 0;
        }
    }
    
    public static boolean hasEnoughColor(Color pColor) {
    	float[] hsbValuesOfColor = RGBtoHSB(pColor.getRed(), pColor.getGreen(), pColor.getBlue(), null);
        Float saturationOfColor = hsbValuesOfColor[1];
        Float brightnessOfColor = hsbValuesOfColor[2];
        return (saturationOfColor >= SATURATION_LOWER_THRESHOLD
                && brightnessOfColor >= BRIGHTNESS_LOWER_THRESHOLD
                && brightnessOfColor <= BRIGHTNESS_UPPER_THRESHOLD);
    }

    public String toString() {
        float[] tHsbValues = RGBtoHSB(this.getRed(), this.getGreen(), this.getBlue(), null);
    	return "h: " + tHsbValues[0] + " s: " + tHsbValues[1] + " b: " + tHsbValues[2];
    }


}
